package org.mq.mymq.common.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.mq.mymq.common.constants.TcpConstants;

/**
 * @ClassName TcpMsgEncoder
 * @Description 消息编码类
 * @Author jiarong_ye
 * @Date 2025/6/19 11:29
 * @Version 1.0
 */
public class TcpMsgEncoder extends MessageToByteEncoder {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        TcpMsg tcpMsg = (TcpMsg) o;
        byteBuf.writeShort(tcpMsg.getMagic());
        byteBuf.writeInt(tcpMsg.getCode());
        byteBuf.writeInt(tcpMsg.getLen());
        byteBuf.writeBytes(tcpMsg.getBody());
        byteBuf.writeBytes(TcpConstants.DEFAULT_DECODE_CHAR.getBytes());
    }
}
